package com.quan.weixin.web.controller.boss;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.base.common.utils.file.FileUpload;
import com.quan.base.common.utils.file.FileUtils;
import com.quan.base.common.utils.file.FileVo;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.weixin.api.API;
import com.quan.weixin.common.WeixinContext;
import com.quan.weixin.common.WeixinUtil;
import com.quan.weixin.rpc.api.WeixinNewsImgService;
import com.quan.weixin.rpc.model.WeixinNewsImg;

@RestController
@RequestMapping(value = "/api/weixin/news_img")
public class WeixinNewsImgController extends BaseController {

	@Autowired
	private WeixinNewsImgService weixinNewsImgService;

	@RequiresPermissions(value="weixin:news_img:add", description="添加微信图文素材图片")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinNewsImg entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinNewsImgService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:delete", description="删除微信图文素材图片")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinNewsImgService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="weixin:news_img:delete", description="删除微信图文素材图片")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.weixinNewsImgService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:update", description="修改微信图文素材图片")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinNewsImg entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinNewsImgService.update(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:detail", description="查询微信图文素材图片详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinNewsImgService.selectById(id);
	}
	
	@RequiresPermissions(value="weixin:news_img:list", description="查询微信图文素材图片")
	@GetMapping(value = "/list")
	public Object list(WeixinNewsImg entity) {
		return this.weixinNewsImgService.selectPage(new Paginator(request), entity) ;
	}
	
	/**
	 * 上传图文消息内的图片
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/uploadNewsImg")
	public Object uploadArticlePic(HttpServletRequest request, String remarks) {
		FileVo fileVo = FileUpload.upload(request, "/weixin/newsImg") ;
		
		// TODO 图片压缩
		
		String newsImgUrl = API.get(API.UPLOAD_NEWS_IMG_URL, WeixinContext.getAccessToken()) ;
		String httpRequest = WeixinUtil.httpRequest(newsImgUrl, new File(fileVo.getFullPath())) ;
		JSONObject parseObject = JSON.parseObject(httpRequest) ;
		String imgUrl = parseObject.get("url").toString() ;
		
		WeixinNewsImg newsImg = new WeixinNewsImg() ;
		newsImg.setRemarks(remarks);
		newsImg.setUrl(imgUrl);
		this.weixinNewsImgService.insertSelective(newsImg) ;
		
		FileUtils.deleteFile(fileVo.getFullPath());
		
		return BaseResult.ok() ;
	}
	
}
